﻿Imports GBDZafiro
Imports System.IO
Imports System.Security.AccessControl

Public Class FrmProductos

    Dim Sep As Char

    Protected Overrides Sub FrmBase_Load(sender As Object, e As System.EventArgs)
        MyBase.FrmBase_Load(sender, e)
        tbFecha.Text = Date.Now.ToLongDateString.ToString
        cargarCmbs()
        cargargvProdBodegas()
        PrimerProd()

    End Sub

    Private Sub cmbGrupo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbGrupo.SelectedIndexChanged
        Try
            cargarcmbSubGrupo()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btprimero_Click(sender As System.Object, e As System.EventArgs) Handles btprimero.Click
        PrimerProd()
    End Sub

    Private Sub btAtras_Click(sender As System.Object, e As System.EventArgs) Handles btAtras.Click

        Dim producto As New GBDZafiro.Producto
        producto.codProducto = tbCodigo.Text

        Dim rowsProd = GBDZafiro.Producto.ConsAnteriorProd(producto).Rows
        If rowsProd Is Nothing Or rowsProd.Count < 1 Then
            'PrimerProd()
            Exit Sub
        End If
        limpiarTextbox()
        For Each rProd As DataRow In rowsProd
            cargarProducto(rProd)
        Next
    End Sub

    Private Sub btAdelante_Click(sender As System.Object, e As System.EventArgs) Handles btAdelante.Click
        Dim producto As New GBDZafiro.Producto
        producto.codProducto = tbCodigo.Text

        Dim rowsProd = GBDZafiro.Producto.ConsSiguienteProd(producto).Rows
        If rowsProd Is Nothing Or rowsProd.Count < 1 Then
            'UltimoProd()
            Exit Sub
        End If
        limpiarTextbox()
        For Each rProd As DataRow In rowsProd
            cargarProducto(rProd)
        Next
    End Sub

    Private Sub btUltimo_Click(sender As System.Object, e As System.EventArgs) Handles btUltimo.Click
        UltimoProd()
    End Sub

    Private Sub pbImagen_Click(sender As System.Object, e As System.EventArgs) Handles pbImagen.Click
        cargarImagen()
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As System.Object, e As System.EventArgs)
        pbImagen.Image = Nothing
        MyBase.bnNuevo_Click(sender, e)
        deshabilitarControles()
        nomControles()
        tbFecha.Text = Date.Now.ToLongDateString.ToString
        tbCodigo.Focus()
    End Sub

    Private Sub tbDescuento_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbDescuento.KeyPress, _
       tbCosto.KeyPress, tbPrecio1.KeyPress, tbStockMax.KeyPress, tbStockMin.KeyPress, _
       tbPeso.KeyPress, tbIVA.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or e.KeyChar.Equals(Sep) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = True
            calUtilidad()
        End If
    End Sub

    Private Sub tbPrecio2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbPrecio2.KeyPress, _
       tbPrecio3.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or e.KeyChar.Equals(Sep) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = True
            calUtilidad(sender)
        End If
    End Sub

    Private Sub tbCosto_Leave(sender As System.Object, e As System.EventArgs) Handles tbCosto.Leave, _
     tbDescuento.Leave, tbPrecio1.Leave, tbStockMax.Leave, _
     tbStockMin.Leave, tbPeso.Leave, tbIVA.Leave
        If validarIva(sender) Then
            Exit Sub
        End If
        If validarDescuento(sender) Then
            Exit Sub
        End If
        'calCostoProm()
        formatMoney(sender)
        calUtilidad()
    End Sub

    Private Sub cmbSubGrupo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSubGrupo.SelectedIndexChanged
        'cargarcmbSubGrupo()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As System.Object, e As System.EventArgs)
        If Not validadCampos() Then
            Exit Sub
        End If
        addUpdateProd()
        MyBase.bnGuardar_Click(sender, e)
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As System.Object, e As System.EventArgs)
        Dim producto As New GBDZafiro.Producto
        producto.codProducto = tbCodigo.Text
        If GBDZafiro.Producto.existProducto(producto) Then
            If msnBox.msnQuestion("¿Esta Seguro De Eliminar El Producto?") = Windows.Forms.DialogResult.Yes Then
                GBDZafiro.Producto.Eliminar(producto)
                msnBox.msnInformacion("Producto Borrado")
                cleargvProdBod()
                dropImagen()
                MyBase.bnEliminar_Click(sender, e)
                PrimerProd()
            End If
        End If
    End Sub

    Protected Overrides Sub bnEditar_Click(sender As System.Object, e As System.EventArgs)
        If Not tbCodigo.Text = "" Then
            MyBase.bnEditar_Click(sender, e)
            deshabilitarControles()
        End If
    End Sub

    Protected Overrides Sub bnCancelar_Click(sender As System.Object, e As System.EventArgs)
        deshabilitarControles()
        pbImagen.Image = Nothing
        cleargvProdBod()
        MyBase.bnCancelar_Click(sender, e)
        nomControles()
    End Sub

    Private Sub tbPrecio2_Leave(sender As System.Object, e As System.EventArgs) Handles tbPrecio2.Leave, _
       tbPrecio3.Leave
        calUtilidad(sender)
    End Sub

    Private Sub btConsulta_Click(sender As System.Object, e As System.EventArgs) Handles btConsulta.Click
        FrmConsutarProd.ShowDialog()
    End Sub

    Private Sub PrimerProd()
        For Each rProd As DataRow In GBDZafiro.Producto.ConsPrimerProd().Rows
            cargarProducto(rProd)
        Next
    End Sub

    Private Sub UltimoProd()
        For Each rProd As DataRow In GBDZafiro.Producto.ConsUltimoProd.Rows
            cargarProducto(rProd)
        Next
    End Sub

    Private Sub cargarProducto(ByRef rProd As DataRow)
        tbCodigo.Text = rProd("COD_PRODUCTO")
        tbDescripcion.Text = rProd("DESCRIPCION")
        tbReferencia.Text = rProd("REFERENCIA")
        cmbPresenUni.SelectedValue = rProd("ID_UNIDAD_MEDIDA")
        cmbTalla.SelectedValue = rProd("COD_TALLA")
        cmbColor.SelectedValue = rProd("COD_COLOR")
        tbPeso.Text = rProd("PESO")
        cmbSubGrupo.SelectedValue = rProd("ID_SUBGRUPO")
        tbStockMax.Text = rProd("STOCK_MAX")
        tbStockMin.Text = rProd("STOCK_MIN")
        tbEstante.Text = rProd("ESTANTE")
        cmbBodega.SelectedValue = rProd("BD_REFERENCIA")
        tbDescuento.Text = rProd("PORC_DCTO").ToString + "%"
        tbCosto.Text = Format(rProd("COSTO_UNIT"), "#,##0.00")
        tbPrecio1.Text = Format(rProd("PRECIO_1"), "#,##0.00")
        If Not rProd("PRECIO_2").ToString.Length < 1 And _
            Not rProd("PRECIO_3").ToString.Length < 1 Then
            tbPrecio2.Text = Format(rProd("PRECIO_2"), "#,##0.00")
            tbPrecio3.Text = Format(rProd("PRECIO_3"), "#,##0.00")
        End If
        tbIVA.Text = rProd("PORC_IVA").ToString() + "%"
        tbCPromedio.Text = rProd("COSTO_PROM").ToString()
        tbUtilidad1.Text = rProd("PORC_UTIL_1").ToString() + "%"
        tbUtilidad2.Text = rProd("PORC_UTIL_2").ToString() + "%"
        tbUtilidad3.Text = rProd("PORC_UTIL_3").ToString() + "%"
        tbExistencia.Text = rProd("EXISTENCIA")

        ''''''' SET Fecha
        Dim fecha As DateTime = rProd("FECHA_REGISTRO")
        tbFecha.Text = fecha.ToLongDateString.ToString()

        ''''''' set Control Desuso
        If rProd("DESUSO") = "S" Then
            cbDesuso.Checked = True
        Else
            cbDesuso.Checked = False
        End If

        ''''''' set Control serial
        If rProd("CTRL_SERIAL") = "S" Then
            cbSolSerialVender.Checked = True
        Else
            cbSolSerialVender.Checked = False
        End If

        ''''''' set Grupo
        Dim subGrupo As New GBDZafiro.SubGrupo
        subGrupo.idSubGrupo = rProd("ID_SUBGRUPO")
        Dim dtsubGrupo As DataTable = GBDZafiro.SubGrupo.ConsxIdSubGrupo(subGrupo)
        For Each rsubGrupo As DataRow In dtsubGrupo.Rows
            cmbGrupo.SelectedValue = rsubGrupo("ID_GRUPO")
        Next

        '''''' set Imagen 
        cargarImagen(rProd("COD_PRODUCTO").ToString())

        cargargvProdBodegas()
    End Sub

    Private Sub addUpdateProd()
        Dim producto As GBDZafiro.Producto = crearProducto()
        If Not GBDZafiro.Producto.existProducto(producto) Then
            GBDZafiro.Producto.Nuevo(producto)
            'msnBox.msnInformacion("Producto Guardado")
        Else
            GBDZafiro.Producto.Actualizar(producto)
            'msnBox.msnInformacion("Producto Actualizado")
        End If
        saveImagen(producto)
        cargargvProdBodegas()
    End Sub

    Private Sub cargarImagen()
        ofdImagen = New OpenFileDialog()
        Try
            ofdImagen.Filter = "Image Files|*.jpg;*.jpeg;*.png;"
            ofdImagen.ShowDialog()
            If Not ofdImagen.FileName.ToString() = "" Then
                Dim image As Image = NonLockingOpen(ofdImagen.FileName) 'System.Drawing.Image.FromFile(ofdImagen.FileName)
                Dim bmap As New Bitmap(image)
                image.Dispose()
                image = Nothing
                pbImagen.Image = bmap

                ofdImagen.Dispose() ''libero el open file dialog
                ofdImagen = Nothing

            End If
        Catch ex As Exception
            msnBox.msnError("formato de imagen no valido")
        End Try
    End Sub

    Private Sub cargarImagen(nombre As String)
        Dim direccion As String = Environment.CurrentDirectory
        direccion += "\imgProducto\" + nombre + ".jpg"
        Try

            If File.Exists(direccion) Then
                Dim imagen As Image = Image.FromFile(direccion)
                Dim bmap As New Bitmap(imagen)
                imagen.Dispose()
                imagen = Nothing
                pbImagen.Image = bmap
            Else
                pbImagen.Image = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub saveImagen(ByRef producto As GBDZafiro.Producto)
        If pbImagen.Image Is Nothing Then
            Exit Sub
        End If

        Try

            Dim direccion As String = Environment.CurrentDirectory
            direccion += "\imgProducto"

            If Not Directory.Exists(direccion) Then
                'trata de crear el folder no existente
                Directory.CreateDirectory(direccion)

                Dim dInfo As New DirectoryInfo(direccion)
                Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
                Dim Regla As FileSystemAccessRule
                Regla = New FileSystemAccessRule(Environment.UserDomainName & "\" & Environment.UserName, _
                                                 FileSystemRights.WriteData, AccessControlType.Allow)
                dSecurity.AddAccessRule(Regla)
                dInfo.SetAccessControl(dSecurity)
            End If

            direccion += "\" + producto.nomArchivo + ".jpg"
            Dim image As Image = pbImagen.Image 'NonLockingOpen(direccion)
            NonLockingSave(image, direccion, System.Drawing.Imaging.ImageFormat.Jpeg)
            'image.Save(direccion + "\" + producto.nomArchivo + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

        Catch ex As Exception
            msnBox.msnError(ex.Message)
        End Try
    End Sub

    Public Shared Function NonLockingOpen(filename As String) As Image
        Dim result As Image

        '#Region "Save file to byte array"

        Dim size As Long = (New FileInfo(filename)).Length
        Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
        Dim data As Byte() = New Byte(size - 1) {}
        Try
            fs.Read(data, 0, CInt(size))
        Finally
            fs.Close()
            fs.Dispose()
        End Try

        '#End Region

        '#Region "Convert bytes to image"

        Dim ms As New MemoryStream()
        ms.Write(data, 0, CInt(size))
        result = New Bitmap(ms)
        ms.Close()

        '#End Region

        Return result
    End Function

    Public Shared Sub NonLockingSave(img As Image, fn As String, format As System.Drawing.Imaging.ImageFormat)
        ' Delete destination file if it already exists
        If File.Exists(fn) Then
            File.Delete(fn)
        End If

        Try

            '#Region "Convert image to destination format"

            Dim ms As New MemoryStream()
            Dim img2 As Image = DirectCast(img.Clone(), Image)
            img2.Save(ms, format)
            img2.Dispose()
            Dim byteArray As Byte() = ms.ToArray()
            ms.Close()
            ms.Dispose()

            '#End Region

            '#Region "Save byte array to file"

            Dim fs As New FileStream(fn, FileMode.CreateNew, FileAccess.Write)
            Try
                fs.Write(byteArray, 0, byteArray.Length)
            Finally
                fs.Close()
                fs.Dispose()

                '#End Region

            End Try
        Catch ex As Exception
            ' Delete file if it was created
            If File.Exists(fn) Then
                File.Delete(fn)
            End If

            ' Re-throw exception
            Throw
        End Try
    End Sub

    Private Sub dropImagen()
        Try
            Dim direccion As String = Environment.CurrentDirectory
            direccion += "\imgProducto"
            If Directory.Exists(direccion) And File.Exists(direccion + "\" + tbCodigo.Text + ".jpg") Then
                My.Computer.FileSystem.DeleteFile(direccion + "\" + tbCodigo.Text + ".jpg")
            End If
        Catch ex As Exception
            msnBox.msnError(ex.Message)
        End Try
    End Sub

    Public Function imageToByteArray(ByVal imageIn As System.Drawing.Image, _
                                     ByVal pformato As System.Drawing.Imaging.ImageFormat) As Byte()

        Dim ms As New IO.MemoryStream

        Try

            imageIn.Save(ms, pformato)

        Catch ex As Exception

            MessageBox.Show("Ocurrió un error " & ex.Message)

        End Try

        Return ms.ToArray()

    End Function

    Private Sub cargargvProdBodegas()
        If Login.usuario Is Nothing Then
            Exit Sub
        End If

        Try
            If Not tbCodigo.TextLength < 1 Then
                cleargvProdBod()

                Dim prodBod As New ProductoBodega
                prodBod.codProducto = tbCodigo.Text
                Dim dtProdBod As DataTable = GBDZafiro.ProductoBodega.ConsxCodProd(prodBod)

                dtProdBod.Columns.Remove("COD_BD_PROD")
                dtProdBod.Columns.Remove("COD_PRODUCTO")

                With gvProdBod
                    .AutoGenerateColumns = True
                    .DataSource = dtProdBod
                    .Columns(0).HeaderText = "Bodega"
                    .Columns(1).HeaderText = "Existencia"
                End With

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub cleargvProdBod()
        gvProdBod.DataSource = Nothing
        gvProdBod.Rows.Clear()
    End Sub

    Private Sub limpiarTextbox()
        For Each ctr As Control In pBase.Controls
            If TypeOf ctr Is TextBox And Not ctr.Name = "tbCodigo" Then
                ctr.Text = ""
            End If
        Next
    End Sub

    Private Function crearProducto() As GBDZafiro.Producto
        Dim producto As New GBDZafiro.Producto
        producto.codUsuario = Login.usuario.codUsuario
        producto.codProducto = tbCodigo.Text.ToUpper()
        producto.descripcion = tbDescripcion.Text.ToUpper()
        producto.referencia = tbReferencia.Text.ToUpper()
        producto.idUniMed = cmbPresenUni.SelectedValue.ToString().ToUpper()
        producto.codTalla = cmbTalla.SelectedValue.ToString().ToUpper()
        producto.codColor = cmbColor.SelectedValue.ToString().ToUpper()
        producto.idSubgrupo = cmbSubGrupo.SelectedValue.ToString().ToUpper()
        producto.estante = tbEstante.Text.ToUpper()
        producto.bdReferencia = cmbBodega.SelectedValue.ToString().ToUpper()

        If tbPeso.TextLength < 1 Then
            producto.peso = Nothing
        Else
            producto.peso = CDec(tbPeso.Text)
        End If
        producto.stockMax = CDec(tbStockMax.Text)
        producto.stockMin = CDec(tbStockMin.Text)
        producto.porcDcto = CDec(tbDescuento.Text.Trim("%"c))
        producto.costoUni = CDec(tbCosto.Text)
        producto.precio1 = CDec(tbPrecio1.Text)
        producto.precio2 = CDec(tbPrecio2.Text)
        producto.precio3 = CDec(tbPrecio3.Text)
        producto.costoProm = CDec(tbCPromedio.Text)
        producto.porcUtil1 = CDec(tbUtilidad1.Text.Trim("%"c))
        producto.porcUtil2 = CDec(tbUtilidad2.Text.Trim("%"c))
        producto.porcUtil3 = CDec(tbUtilidad3.Text.Trim("%"c))
        producto.porcIVA = CDec(tbIVA.Text.Trim("%"c))

        If cbDesuso.Checked Then
            producto.desuso = "S"
        Else
            producto.desuso = "N"
        End If

        If cbSolSerialVender.Checked Then
            producto.ctrlSerial = "S"
        Else
            producto.ctrlSerial = "N"
        End If

        'producto.nomArchivo = System.IO.Path.GetFileNameWithoutExtension(ofdImagen.FileName).ToString()
        producto.nomArchivo = producto.codProducto
        Return producto
    End Function

    Private Function validadCampos() As Boolean
        For Each ctr As Control In pBase.Controls

            If TypeOf ctr Is TextBox Or TypeOf ctr Is ComboBox Then
                If ctr.Text.Length < 1 Or ctr.Text Is Nothing Then

                    If ctr.Name.ToString() = "tbReferencia" Or ctr.Name.ToString() = "tbEstante" Or _
                       ctr.Name.ToString() = "tbPeso" Then
                        Return True
                        Exit Function
                    End If

                    msnBox.msnExclamacion("Complete todos los campos")
                    Return False
                    Exit Function
                End If
            End If

        Next
        Return True
    End Function

    Private Sub deshabilitarControles()
        tbCPromedio.Enabled = False
        tbUtilidad1.Enabled = False
        tbUtilidad2.Enabled = False
        tbUtilidad3.Enabled = False
        tbFecha.Enabled = False
        tbFecha.Clear()
        cbDesuso.Checked = False
        cbSolSerialVender.Checked = False
    End Sub

    Private Sub formatMoney(sender As System.Object)
        Dim bt As TextBox = DirectCast(sender, TextBox)
        If bt.TextLength < 1 Then
            Exit Sub
        End If
        bt.Text = Format(CType(bt.Text, Decimal), "#,##0.00")
    End Sub

    Private Sub formatMoney(tb As TextBox)
        If tb.TextLength < 1 Then
            Exit Sub
        End If
        tb.Text = Format(CType(tb.Text, Decimal), "#,##0.00")
    End Sub

    Private Sub calUtilidad(sender As System.Object)
        Dim tb As TextBox = DirectCast(sender, TextBox)
        If tb.TextLength < 1 Then
            Exit Sub
        End If

        Dim precio1 As Decimal = CDec(tbPrecio1.Text)
        Dim precio2 As Decimal = CDec(tbPrecio2.Text)
        Dim precio3 As Decimal
        If Not tbPrecio3.TextLength < 1 Then
            precio3 = CDec(tbPrecio3.Text)
        End If

        Dim costo As Decimal = CDec(tbCosto.Text)

        If tb.Name.ToString() = "tbPrecio2" Then
            If precio2 < precio1 Then
                msnBox.msnExclamacion("El precio 2 debe ser mayor que el precio 1")
                tb.Focus()
                Exit Sub
            End If

            tbUtilidad2.Text = Math.Round(100 - ((100 * costo) / precio2), 1).ToString() + "%"
            tbPrecio3.Clear()
            tbUtilidad3.Clear()

        Else
            If precio3 < precio2 Then
                msnBox.msnExclamacion("El precio 3 debe ser mayor que el precio 2")
                tb.Focus()
                Exit Sub
            End If
            tbUtilidad3.Text = Math.Round(100 - ((100 * costo) / precio3), 1).ToString() + "%"
        End If

        formatMoney(tb)
    End Sub

    Private Sub calUtilidad()
        If tbCosto.TextLength < 1 Or tbPrecio1.TextLength < 1 Then
            Exit Sub
        End If

        Dim precio1 As Decimal = CDec(tbPrecio1.Text)
        Dim precio2 As Decimal = precio1 + (precio1 * 0.1)
        Dim precio3 As Decimal = precio1 + (precio1 * 0.2)
        Dim costo As Decimal = CDec(tbCosto.Text)

        tbPrecio2.Text = precio2.ToString()
        tbPrecio3.Text = precio3.ToString()
        tbUtilidad1.Text = Math.Round(100 - ((100 * costo) / precio1), 1).ToString() + "%"
        tbUtilidad2.Text = Math.Round(100 - ((100 * costo) / precio2), 1).ToString() + "%"
        tbUtilidad3.Text = Math.Round(100 - ((100 * costo) / precio3), 1).ToString() + "%"

        formatMoney(tbPrecio2)
        formatMoney(tbPrecio3)
    End Sub

    Private Sub calCostoProm()
        If Not tbCPromedio.TextLength < 1 And Not tbCosto.TextLength < 1 Then
            Dim desc, costo As Decimal
            desc = CDec(tbDescuento.Text)
            costo = CDec(tbCosto.Text)
            tbCPromedio.Text = (costo - desc).ToString()
        End If
    End Sub

    Private Function validarIva(sender As System.Object) As Boolean
        Dim tb As TextBox = DirectCast(sender, TextBox)
        If tb.Name = "tbIVA" Then
            If tb.TextLength < 1 Or tb.Text = "%" Then
                tb.Clear()
            Else
                tbIVA.Text = tbIVA.Text.Trim("%"c)
                formatMoney(sender)
                If CType(tbIVA.Text, Decimal) > 100 Then
                    msnBox.msnExclamacion("El Iva debe ser menor a 100")
                    tbIVA.Clear()
                    tbIVA.Focus()
                    Return True
                    Exit Function
                End If
                tbIVA.Text = tbIVA.Text + "%"
                Return True
                Exit Function
            End If
        End If
        Return False
    End Function

    Private Function validarDescuento(sender As System.Object) As Boolean
        Dim tb As TextBox = DirectCast(sender, TextBox)
        If tb.Name = "tbDescuento" Then
            If tb.TextLength < 1 Or tb.Text = "%" Then
                tb.Clear()
            Else
                tbDescuento.Text = tbDescuento.Text.Trim("%"c)
                formatMoney(sender)
                If CType(tbDescuento.Text, Decimal) > 100 Then
                    msnBox.msnExclamacion("El Descuento debe ser menor a 100")
                    tbDescuento.Clear()
                    tbDescuento.Focus()
                    Return True
                    Exit Function
                End If
                tbDescuento.Text = tbDescuento.Text + "%"
                Return True
                Exit Function
            End If
        End If
        Return False
    End Function

    Private Sub cargarCmbs()
        cargarcmbGupo()
        cargarcmbSubGrupo()
        CargarCmbUndMed()
        CargarCmbUndMed()
        cargarCmbTalla()
        cargarCmbColor()
        nomControles()
        cargarcmbBd()
    End Sub

    Private Sub cargarcmbGupo()
        Dim dtGrupo As DataTable = GBDZafiro.Grupo.ConsultarAll()
        cmbGrupo.DataSource = dtGrupo
        cmbGrupo.DisplayMember = "DESCRIPCION"
        cmbGrupo.ValueMember = "ID_GRUPO"
    End Sub

    Private Sub cargarcmbSubGrupo()
        Dim subGrupo As New GBDZafiro.SubGrupo
        subGrupo.idGrupo = cmbGrupo.SelectedValue.ToString()
        Dim dtSubGrupo As DataTable = GBDZafiro.SubGrupo.ConsxIdGrupo(subGrupo)
        cmbSubGrupo.DataSource = dtSubGrupo
        cmbSubGrupo.DisplayMember = "DESCRIPCION"
        cmbSubGrupo.ValueMember = "ID_SUBGRUPO"
    End Sub

    Private Sub CargarCmbUndMed()
        Dim dtUniMed As DataTable = GBDZafiro.UnidadMedida.ConsultarAll()
        cmbPresenUni.DataSource = dtUniMed
        cmbPresenUni.DisplayMember = "ID_UNIDAD_MEDIDA"
        cmbPresenUni.ValueMember = "ID_UNIDAD_MEDIDA"
    End Sub

    Private Sub cargarCmbTalla()
        Dim dtTalla As DataTable = GBDZafiro.Talla.ConsultarAll()
        cmbTalla.DataSource = dtTalla
        cmbTalla.DisplayMember = "COD_TALLA"
        cmbTalla.ValueMember = "COD_TALLA"
    End Sub

    Private Sub cargarCmbColor()
        Dim dtColor As DataTable = GBDZafiro.Color.ConsultarAll()
        cmbColor.DataSource = dtColor
        cmbColor.DisplayMember = "COLOR"
        cmbColor.ValueMember = "COD_COLOR"
    End Sub

    Private Sub cargarcmbBd()
        Dim dtbodega As DataTable = GBDZafiro.Bodega.ConsultarAll()
        cmbBodega.DataSource = dtbodega
        cmbBodega.DisplayMember = "COD_BODEGA"
        cmbBodega.ValueMember = "COD_BODEGA"

    End Sub

    Private Sub nomControles()
        cbSolSerialVender.Text = "Solicitar serial al vender"
        tbCPromedio.Text = "0"
        cbDesuso.Text = "Producto en Desuso"
    End Sub

    Private Sub ctrsConsultarProducto()
        pBase.Visible = False
        btNuevo.Visible = False
        btGuardar.Visible = False
        btEditar.Visible = False
        btCancelar.Visible = False
        btEliminar.Visible = False
        btAdelante.Visible = False
        btAtras.Visible = False
        btprimero.Visible = False
        btUltimo.Visible = False
        tbFecha.Visible = False

    End Sub

    Private Sub ctrsRgisProducto()
        pBase.Visible = True
        btNuevo.Visible = True
        btGuardar.Visible = True
        btEditar.Visible = True
        btCancelar.Visible = True
        btEliminar.Visible = True
        btAdelante.Visible = True
        btAtras.Visible = True
        btprimero.Visible = True
        btUltimo.Visible = True
        tbFecha.Visible = True

    End Sub

End Class
